package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.YouxuanAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.YouxuanBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class YouxuanActivity extends BaseActivity {
    private ImageView icon_back;
    private RecyclerView rc_youxuan;
    private String token;
    private YouxuanAdapter adapter;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_youxuan);
    }

    @Override
    protected void initView() {
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        icon_back = findViewById(R.id.icon_back);
        rc_youxuan = findViewById(R.id.rc_youxuan);
        getTitleHosts(token, EXTRA);
    }

    private void getTitleHosts(String token, String extra) {
        getRequest();
        NetUtils2.getInstance().getApiService().getTitleHosts(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr", decryptStr);
                        Gson gson = new Gson();
                        YouxuanBean bean = gson.fromJson(decryptStr, YouxuanBean.class);

                        LinearLayoutManager layoutManager = new GridLayoutManager(YouxuanActivity.this, 2);
                        rc_youxuan.setLayoutManager(layoutManager);
                        if (bean != null) {
                            if (bean.getData() != null) {
                                adapter = new YouxuanAdapter(bean.getData().getHosts());
                                rc_youxuan.setAdapter(adapter);
                                adapter.setOnAddClickListener(new YouxuanAdapter.OnAddClickListener() {
                                    @Override
                                    public void onItemClicks(int position, int userid, String userName,String icon) {
                                        if (bean.getData().getHosts().get(position).getCoverType() == 0) {
                                            //跳转图片
                                            Intent intent = new Intent(YouxuanActivity.this, DetailsActivity.class);
                                            intent.putExtra("userid", userid);
                                            startActivity(intent);
                                        } else {
                                            //跳转视频
                                            Intent intent = new Intent(YouxuanActivity.this, VedioActivity.class);
                                            intent.putExtra("userid", userid);
                                            intent.putExtra("username", userName);
                                            intent.putExtra("usericon",icon);
                                            intent.putExtra("urls", "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/" + bean.getData().getHosts().get(position).getCover());
                                            startActivity(intent);
                                        }
                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
